package com.yesido.javabase.io.demo1;

import com.yesido.javabase.io.FileConstant;

import java.io.FileInputStream;
import java.io.IOException;

/**
 * @author yesido
 * @description java.io包下的文件的读写
 * @date 2023-06-28 16:24
 * @Version 8.3
 */
public class JavaIo {

    public static void main(String[] args) throws IOException {
       /* FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(FileConstant.OUT_FILE_PATH);
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < 5000; i++) {
                sb.append("write_").append(i).append("\n");
            }

            byte[] bytes = sb.toString().getBytes();
            fos.write(bytes);
            System.out.println("write success...");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            fos.close();
        }*/

        // 读取文件
        readFile();

    }

    private static void readFile() {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(FileConstant.OUT_FILE_PATH);
            byte[] buffer = new byte[1024];
            int bytesRead = fis.read(buffer);
            while (bytesRead != -1) {
                for (int i = 0; i < bytesRead; i++) {
                    //System.out.println("文件内容： " + buffer[i]);
                    String content = new String(buffer, 0, bytesRead);
                    System.out.println("文件内容： " + content);
                }
                bytesRead = fis.read(buffer);
            }
//
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
